package com.fufufu.mail.ware.service.impl;

import com.fufufu.common.constant.WareConstant;
import com.fufufu.mail.ware.vo.DoneVoItem;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fufufu.common.utils.PageUtils;
import com.fufufu.common.utils.Query;

import com.fufufu.mail.ware.dao.PurchaseDetailDao;
import com.fufufu.mail.ware.entity.PurchaseDetailEntity;
import com.fufufu.mail.ware.service.PurchaseDetailService;
import org.springframework.util.StringUtils;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        String wareId = (String) params.get("wareId");
        if (StringUtils.hasText(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }
        String status = (String) params.get("status");
        if (StringUtils.hasText(status)) {
            queryWrapper.eq("status", status);
        }
        String key = (String) params.get("key");
        if (StringUtils.hasText(key)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("purchase_id", key).or().eq("sku_id", key);
            });
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateBatchBypurchaseId(List<Long> purchaseIds) {
        if (purchaseIds == null || purchaseIds.size() == 0) {
            return;
        }
        // 找出所有purchaseIds对应的detailEntities
        List<PurchaseDetailEntity> detailEntities = this.baseMapper.selectList(
                new QueryWrapper<PurchaseDetailEntity>().in("purchase_id", purchaseIds));

        List<PurchaseDetailEntity> PurchaseDetailEntity = detailEntities.stream().map((detail) -> {
            // 这里进行这么麻烦的操作是为了减少数据库字段的更新，如果直接用peek把detail的状态改了，那么数据库就会执行很多无用的更新
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(detail.getId());
            purchaseDetailEntity.setStatus(WareConstant.purchaseDetailEnum.BUYING.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        this.updateBatchById(PurchaseDetailEntity);
    }

    @Override
    public boolean doneDetail(List<DoneVoItem> items, List<PurchaseDetailEntity> ans) {
        AtomicBoolean allFinish = new AtomicBoolean(true);
        List<PurchaseDetailEntity> purchaseDetailEntities = items.stream().map((item) -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(item.getItemId());
            purchaseDetailEntity.setStatus(item.getStatus());
            if (item.getStatus() == WareConstant.purchaseDetailEnum.FAILUER.getCode()) {
                allFinish.set(false);
            } else {
                ans.add(purchaseDetailEntity);
            }
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        this.updateBatchById(purchaseDetailEntities);
        return allFinish.get();
    }
}